//import org.codehaus.groovy.grails.plugins.springsecurity.Secured

class PersonService {
    boolean transactional = false

    def authenticateService

    def getContactPerson(company) {
        def contacts = company.contacts
        def contact = Person.findAllByUser(user)
        contact.retainAll(contacts)
        return contact
    }

    def Person getPersonForCurrentUser() {
        return Person.findByUser(authenticateService.userDomain()) as Person
    }

    def getRepsForCurrentUser() {
        return Representative.fndAllByPerson(getPersonForCurrentUser())
    }

    def getRepForCurrentUser(String type) {
        def rep = Representative.executeQuery(
                "SELECT rep\
                FROM Representative rep, Person person, User user\
                WHERE user.id = ${authenticateService.userDomain().id}\
                AND rep.title = '${type}'\
                AND rep.person = user.person")
        if (!rep) {
            return
        }
        return rep[0]
    }

    def getCompanyRep(company, user) {
        if (!company) {
            return
        }

        def reps = company.reps as List
        if (!reps) {
            return
        }

        if (!user) {
            return reps[0]
        }

        return getRepForCurrentUser(Representative.SALES)
    }
}
